
//一般直接写在一个js文件中
layui.config({
    base: '/script/'
});
layui.use(['element', 'form', 'layer', 'myStorage'], function () {
    var $ = layui.jquery,
        element = layui.element, //加载element模块
        layer = layui.layer, //加载layer模块
        storage = layui.myStorage;

    /* AJAX请求默认选项，处理连接超时问题 */
    $.ajaxSetup({
        beforeSend: function () {
            layer.load(0);
        },
        complete: function (xhr) {
            layer.closeAll('loading');
            if (xhr.status === 401) {
                layer.confirm('session连接超时，是否重新登录？', {
                    btn: ['是', '否']
                }, function () {
                    if (window.parent.window !== window) {
                        window.top.location = window.location.pathname + '/login';
                    }
                });
            }
        }
    });
    /*  漂浮消息 */
    $.fn.Messager = function (result) {
        if (result.code === 200) {
            layer.msg(result.msg, {offset: '15px', time: 3000, icon: 1});

            setTimeout(function () {
                if (result.data === 'submit[refresh]') {
                    parent.location.reload();
                    return;
                }
                if (result.data != null) {
                    window.location.href = result.data;
                    //所有的跳转都要携带token
                    // getByToken(result.data);
                } else {
                    window.location.reload();
                }
            }, 2000);
        } else {
            layer.msg(result.msg, {offset: '15px', time: 3000, icon: 2});
        }
    };

    // 登录成功做跳转 并获取token
    $.fn.loginSuccess = function loginSuccess(result) {
        if (result.code === 200) {
            layer.msg(result.msg, {offset: '15px', time: 3000, icon: 1});
            if (result.data != null) {
                setTimeout(function () {
                    $.ajax({
                        type: "GET",
                        url: result.data, //main.html
                        headers: {		//这里将存储的token取出发送至后端
                            // Authorization: sessionStorage.getItem("token")
                            Authorization: storage.getItem('token')
                        },
                        success: function (result, textStatus, request) {
                            console.log("ajaxSuccess")
                            // window.location.href = result.data;
                        },
                    });
                }, 2000);
            }
        } else {
            layer.msg(result.msg, {offset: '15px', time: 3000, icon: 2});
        }
    };

    // 前端ajax发送token
    function getByToken(url) {
        $.ajax({
            type: "GET",
            async: false,
            url: "main",
            // contentType: "application/json; charset=utf-8",
            // dataType: "json",
            headers: {		//这里将存储的token取出发送至后端
                Authorization: storage.getItem('token')
            },
            success: function (result, textStatus, request) {
                console.log("ajaxSuccess", result)
                // window.location.href = "main";
            }
        });
    };

    /* 提交表单数据 */
    $(document).on("click", ".ajax-submit", function (e) {
        e.preventDefault();
        var form = $(this).parents("form");
        var url = form.attr("action");
        var serializeArray = form.serializeArray();
        $.post(url, serializeArray, function (result) {
            if (result.data == null) {
                result.data = 'submit[refresh]';
            }
            $.fn.Messager(result);
        });
    });

    /*iframe内容切换 */
    $("dd>a").click(function (e) {
        //设置a的点击事件不跳转到另一个页面
        e.preventDefault();
        //给iframe添加src属性，值为a的href值
        $("#iframeMain").attr("src", $(this).attr("href"));
    });

    $(document).ready(function () {

        if (storage.getItem("token") === undefined) {
            $.get("/logout",function (result) {
                console.log("长时间未操作页面")
            })
        };
        /* 用户标签赋值*/
        $(document).ready(function () {
            var username = storage.getItem("username");
            $('#title-user').text(username);
        });
    });

});